package dao.inventory;

import dao.BaseDaoHibernate;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import model.inventory.ItemBonusId;
import model.inventory.ItemDiskonId;
import model.inventory.ItemHpp;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class ItemHppDao extends BaseDaoHibernate<ItemHpp> {
    
    private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ItemHppDao.class);
    
    public boolean saveData(ItemHpp itemHpp) {
        Session session = sessionFactory.openSession();
        boolean saved = false;
        try {
            session.beginTransaction();
            ItemHpp newItemHpp = (ItemHpp) sessionFactory.getCurrentSession().get(ItemHpp.class, itemHpp.getId());
            
            boolean hppExist = newItemHpp!=null; 
            if (hppExist) {
                newItemHpp = itemHpp;
               // itemHpp=null;
            }
            else {
                newItemHpp = ItemHpp.clone(itemHpp);
            }
            
            for (int i=0; i<newItemHpp.getItemDiskons().size(); i++) {
                newItemHpp.getItemDiskons().get(i).setItemHpp(newItemHpp);
                
                if (hppExist) {
                    newItemHpp.getItemDiskons().get(i).getId().setItemId(newItemHpp.getId().getItemId());
                    newItemHpp.getItemDiskons().get(i).getId().setStartDate(newItemHpp.getId().getStartDate());
                    newItemHpp.getItemDiskons().get(i).getId().setSupplierId(newItemHpp.getId().getSupplierId());
                    newItemHpp.getItemDiskons().get(i).getId().setSeq(i);
                    
                } else {
                    newItemHpp.getItemDiskons().get(i).setId(new ItemDiskonId(
                        newItemHpp.getId().getItemId(), newItemHpp.getId().getStartDate(), newItemHpp.getId().getSupplierId(), i));
                }
            }
            
            for (int i=0; i<newItemHpp.getItemBonuses().size(); i++) {
                newItemHpp.getItemBonuses().get(i).setItemHpp(newItemHpp);
                
                if (hppExist) {
                    newItemHpp.getItemBonuses().get(i).getId().setItemId(newItemHpp.getId().getItemId());
                    newItemHpp.getItemBonuses().get(i).getId().setStartDate(newItemHpp.getId().getStartDate());
                    newItemHpp.getItemBonuses().get(i).getId().setSupplierId(newItemHpp.getId().getSupplierId());
                    newItemHpp.getItemBonuses().get(i).getId().setSeq(i);
                    
                } else {
                    newItemHpp.getItemBonuses().get(i).setId(new ItemBonusId(
                        newItemHpp.getId().getItemId(), newItemHpp.getId().getStartDate(), newItemHpp.getId().getSupplierId(), i));
                }
            }
            
            session.clear();
            session.evict(itemHpp);
            
            session.saveOrUpdate(newItemHpp);
         
            session.getTransaction().commit();
            saved = true;
        } catch (RuntimeException e) {
            session.getTransaction().rollback(); 
            log.error("gagal", e);
            java.util.logging.Logger.getLogger(ItemHppDao.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
            saved = false;
        } finally {
            return saved;
        }
    }
    
    public boolean saveData(List<ItemHpp> itemHpps, int newSupplier, boolean isCopy) {
        Session session = sessionFactory.openSession();
        boolean saved = false;
        try {
            session.beginTransaction();
            for (ItemHpp hpp : itemHpps) {
                
                CallableStatement sp = session.connection().prepareCall("{ call inventory.copy_price(?, ?, ?, ?) }");
                sp.setInt(1, hpp.getId().getItemId());
                sp.setInt(2, hpp.getId().getSupplierId());
                sp.setInt(3, newSupplier);
                sp.setBoolean(4, isCopy);
                sp.executeUpdate();
            }
            
            session.getTransaction().commit();
            saved=true;
        } catch (RuntimeException | SQLException e) {
            session.getTransaction().rollback(); 
            saved=false;
            log.error("gagal", e);
            java.util.logging.Logger.getLogger(ItemHppDao.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
        } finally {
            if (session != null) { session.close();}
            return saved;
        }
    }
    
    public List<Date> getDates(int itemId, int supplierId) {
        return sessionFactory.getCurrentSession().createQuery(
                "select distinct id.startDate from ItemHpp where id.itemId = "+itemId+
                " and id.supplierId = "+supplierId+
                " order by id.startDate desc").list();
    }
        

}
